package com.wsy;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

public class connsqlserver {

	/** 
	 *   @param   args 
	 */
	private static Connection cn = null;

	private void getConnection() {
		if (cn != null) {
			return;
		}

		String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
		String dbURL = "jdbc:microsoft:sqlserver://192.168.2.92:1433;DatabaseName=db_net";
		String userName = "sa";
		
		String userPwd = "84411360@123.com";

		try {
			Class.forName(driverName);
			cn = DriverManager.getConnection(dbURL, userName, userPwd);
			System.out.println("Connection Successful!");
		} catch (Exception e) {
			e.printStackTrace();
		}

		return;
	}

	public ResultSet executeQuery(String sql) {
		if (cn == null)
			getConnection();
		try {
			return cn.createStatement().executeQuery(sql);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		} finally {
		}
	}

	public int executeUpdate(String sql) {
		if (cn == null)
			getConnection();
		try {
			return cn.createStatement().executeUpdate(sql);
		} catch (SQLException e) {
			e.printStackTrace();
			return -1;
		} finally {
		}
	}

	public void close() {
		try {
			cn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			cn = null;
		}
	}
}
